<template>
  <div>
    <upload-excel :on-success="handleSuccess" :before-upload="beforeUpload" />
    <el-table :data="tableData" border highlight-current-row style="width: 100%;margin-top:20px;">
      <el-table-column v-for="item of tableHeader" :key="item" :prop="item" :label="item" />
    </el-table>
    <!-- 父传子练习 -->
    <son-component :money="100" :gcfn="gcfn" />
  </div>
</template>

<script>
import sonComponent from './components/son-component.vue' // 练习父传子
// import UploadExcelComponent from '@/components/UploadExcel/index.vue'

export default {
  components: { sonComponent },
  // name: 'UploadExcel',  //不注释 报错栈溢出
  // components: { UploadExcelComponent },
  data() {
    return {
      tableData: [],
      tableHeader: []
    }
  },
  methods: {
    gcfn(a, b, c, d) {
      console.log(a, b, c, d)
    },
    // 上传前的回调 可以限制上传文件大小 等
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1

      if (isLt1M) {
        return true // 小于1mb 返回true
      }

      // 大于1mb错误提示
      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning'
      })
      return false // 大于1mb返回false
    },
    // header 表格头 results对象数组
    handleSuccess({ results, header }) {
      this.tableData = results
      this.tableHeader = header
    }
  }
}
</script>
